package org.ihelpuoo.io;

import java.io.IOException;
import java.io.InputStream;

public class CharInputStream extends InputStream
{

	private byte[]	chars;
	private int		cursor;
	private int		mark;

	private CharInputStream()
	{
		super();
		cursor = 0;
		mark = 0;
	}

	public CharInputStream(CharSequence s)
	{
		this();
		if (null != s)
			chars = s.toString().getBytes();
		else
			chars = new byte[0];
	}

	@Override
	public synchronized void mark(int readlimit)
	{
		this.mark = readlimit;
	}

	@Override
	public boolean markSupported()
	{
		return true;
	}

	@Override
	public int read() throws IOException
	{
		if (cursor >= chars.length)
			return -1;
		return chars[cursor++];
	}

	@Override
	public int available() throws IOException
	{
		return chars.length;
	}

	@Override
	public synchronized void reset() throws IOException
	{
		cursor = mark;
	}

	@Override
	public long skip(long n) throws IOException
	{
		int len = chars.length;
		if (len > cursor + n)
		{
			cursor += n;
			return n;
		}
		int d = len - 1 - cursor;
		cursor = len;
		return d;
	}

}
